#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>

int main()
{
  //int fd = open("log.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
  int fd = open("log.txt", O_WRONLY | O_CREAT | O_APPEND, 0666);
  if(fd < 0)
  {
    perror("open error");
    return -1;
  }
  dup2(fd,1);
  printf("open fd: %d\n",fd);//向stdout输出，由于关闭了标准输出文件，无法输出
  fprintf(stdout, "open fd: %d\n",fd);//向stdout输出，
  
  const char* msg = "hello world!";
  write(1,msg,strlen(msg));
  

  fflush(stdout);//重新刷新
  close(fd);
  return 0;
}
